package com.example.musicplayer.settings;

import com.example.musicplayer.DataLoadManager;
import com.example.musicplayer.MusicPlayerApplication;
import com.example.musicplayer.R;
import com.example.musicplayer.commonUtils.DisplayUtil;

public class DesktopLyricSettings {
    public static final int LYRIC_SIZE_1 = 1;
    public static final int LYRIC_SIZE_2 = 2;
    public static final int LYRIC_SIZE_3 = 3;
    public static final int LYRIC_SIZE_4 = 4;
    public static final int LYRIC_SIZE_5 = 5;
    public static final int LYRIC_SIZE_6 = 6;
    public static final int LYRIC_SIZE_7 = 7;
    public static final int LYRIC_SIZE_8 = 8;
    public static final int LYRIC_SIZE_9 = 9;

    public static final int LYRIC_COLOR_1 = 1;
    public static final int LYRIC_COLOR_2 = 2;
    public static final int LYRIC_COLOR_3 = 3;
    public static final int LYRIC_COLOR_4 = 4;
    public static final int LYRIC_COLOR_5 = 5;

    private int mLyricSize = LYRIC_SIZE_5;
    private int mLyricColor = LYRIC_COLOR_1;
    private int mBackgroundColor;
    private DataLoadManager mLoadManager;

    public DesktopLyricSettings(DataLoadManager loadManager) {
        mLoadManager = loadManager;
        if (mLoadManager == null) {
            mLoadManager = DataLoadManager.getInstance();
        }
        if (mLoadManager != null) {
            mLyricSize = mLoadManager.readOneBaseData(DataLoadManager.DESKTOP_LYRIC_SIZE, mLyricSize);
            mLyricColor = mLoadManager.readOneBaseData(DataLoadManager.DESKTOP_LYRIC_COLOR, LYRIC_COLOR_1);
            mBackgroundColor = mLoadManager.readOneBaseData(DataLoadManager.DESKTOP_LYRIC_BACKGROUND,
                    MusicPlayerApplication.getInstance().getResources().getColor(R.color.desktop_lyric_background));
        }
    }

    public void changeLyricSize(boolean isIncrease) {
        if (isIncrease && mLyricSize < LYRIC_SIZE_9) {
            mLyricSize++;
        }
        if (!isIncrease && mLyricSize > LYRIC_SIZE_1) {
            mLyricSize--;
        }
        if (mLoadManager != null) {
            mLoadManager.writeOneBaseData(DataLoadManager.DESKTOP_LYRIC_SIZE, mLyricSize);
        }
    }

    public void changeLyricColor(int lyricColor) {
        if (lyricColor >= LYRIC_COLOR_1 && lyricColor <= LYRIC_COLOR_5) {
            mLyricColor = lyricColor;
            if (mLoadManager != null) {
                mLoadManager.writeOneBaseData(DataLoadManager.DESKTOP_LYRIC_COLOR, mLyricColor);
            }
        }
    }

    public void changeBackgroundColor(int backgroundColor) {
        if (mLoadManager != null) {
            mBackgroundColor = backgroundColor;
            mLoadManager.writeOneBaseData(DataLoadManager.DESKTOP_LYRIC_BACKGROUND, backgroundColor);
        }
    }

    public int getLyricTextSize() {
        int sizeId;
        switch (mLyricSize) {
            case LYRIC_SIZE_1:
                sizeId = R.dimen.desktop_lyric_size_1;
                break;
            case LYRIC_SIZE_2:
                sizeId = R.dimen.desktop_lyric_size_2;
                break;
            case LYRIC_SIZE_3:
                sizeId = R.dimen.desktop_lyric_size_3;
                break;
            case LYRIC_SIZE_4:
                sizeId = R.dimen.desktop_lyric_size_4;
                break;
            case LYRIC_SIZE_6:
                sizeId = R.dimen.desktop_lyric_size_6;
                break;
            case LYRIC_SIZE_7:
                sizeId = R.dimen.desktop_lyric_size_7;
                break;
            case LYRIC_SIZE_8:
                sizeId = R.dimen.desktop_lyric_size_8;
                break;
            case LYRIC_SIZE_9:
                sizeId = R.dimen.desktop_lyric_size_9;
                break;
            case LYRIC_SIZE_5:
            default:
                sizeId = R.dimen.desktop_lyric_size_5;
                break;
        }
        int textSize = MusicPlayerApplication.getInstance().getResources().getDimensionPixelSize(sizeId);
        //px change to sp
        return DisplayUtil.px2sp(MusicPlayerApplication.getInstance(), textSize);
    }

    public int getLyricTextColor() {
        int colorId;
        switch (mLyricColor) {
            case LYRIC_COLOR_2:
                colorId = R.color.desktop_lyric_color_2;
                break;
            case LYRIC_COLOR_3:
                colorId = R.color.desktop_lyric_color_3;
                break;
            case LYRIC_COLOR_4:
                colorId = R.color.desktop_lyric_color_4;
                break;
            case LYRIC_COLOR_5:
                colorId = R.color.desktop_lyric_color_5;
                break;
            case LYRIC_COLOR_1:
            default:
                colorId = R.color.desktop_lyric_color_1;
                break;
        }
        return MusicPlayerApplication.getInstance().getResources().getColor(colorId);
    }

    public int getLyricColor() {
        return mLyricColor;
    }

    public int getBackgroundColor() {
        return mBackgroundColor;
    }
}